import 'package:flutter/material.dart';

class AnimaPage3 extends StatefulWidget {
  @override
  _AnimaPage3State createState() => _AnimaPage3State();
}

class _AnimaPage3State extends State<AnimaPage3> {
  IconData iconData = Icons.clear;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('按键切换动画效果'),
        actions: <Widget>[
          AnimatedSwitcher(
            transitionBuilder: (child, anim) =>
                ScaleTransition(child: child, scale: anim),
            duration: Duration(milliseconds: 300),
            child: IconButton(
              key: ValueKey(iconData),
              icon: Icon(iconData),
              onPressed: () {
                setState(() {
                  if (iconData == Icons.clear) {
                    iconData = Icons.add;
                  } else {
                    iconData = Icons.clear;
                  }
                });
              },
            ),
          ),
        ],
      ),
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (iconData == Icons.clear) {
              iconData = Icons.add;
            } else {
              iconData = Icons.clear;
            }
          });
        },
        child: AnimatedSwitcher(
          transitionBuilder: (child, anim) =>
              ScaleTransition(child: child, scale: anim),
          duration: Duration(milliseconds: 300),
          child: IconButton(
            key: ValueKey(iconData),
            icon: Icon(iconData),
            onPressed: () {
              setState(() {
                if (iconData == Icons.clear)
                  iconData = Icons.add;
                else
                  iconData = Icons.clear;
              });
            },
          ),
        ),
      ),
    );
  }
}
